package com.user.controller;

import com.common.MyPage;
import com.common.PagedResult;
import com.user.UserQueryCondition;
import com.user.domain.User;
import com.user.repository.UserRepository;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static org.slf4j.LoggerFactory.getLogger;


/**
 * Created by sola on 2017/9/23.
 */
@RestController
@RequestMapping(value = "/userController")
public class UserController {
    private static final Logger logger = getLogger(UserController.class);

    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/useradd" ,method = RequestMethod.POST)
    public void addUser(HttpServletRequest httpRequest, HttpServletResponse httpResponse){

        System.out.print("addUser");
    }

    @RequestMapping(value = "/userlist")
    public Iterable<User> listUser(){
        return userRepository.findAll();
    }

    @RequestMapping(value = "/userlistpaged")
    public PagedResult<User> listUserPaged(@RequestBody UserQueryCondition queryCondition){
        Page<User> userPage = userRepository.findAll(queryCondition);
        final List<User> userList= userPage.getContent();
        return new PagedResult<User>(userPage.getTotalElements(),userList);
    }

}
